package org.jwos.plugin.file.web;

import java.util.Locale;

import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.jwos.platform.domain.GlobalConfig;
import org.jwos.platform.domain.Plugin;
import org.jwos.platform.domain.PluginResource;
import org.jwos.platform.domain.User;
import org.jwos.platform.service.PluginManager;
import org.jwos.platform.service.SessionManager;
import org.jwos.platform.service.UserManager;
import org.jwos.plugin.file.jcr.util.RepositorySessionFactory;
import org.jwos.plugin.file.util.RepositoryUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class ContextListener extends ContextLoaderListener implements
		ServletContextListener {

	protected static Logger logger = Logger.getLogger(ContextListener.class);

	public void contextInitialized(ServletContextEvent event) {
		logger.debug("server starting");
		
		super.contextInitialized(event);
		
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		PluginManager pluginManager = (PluginManager)context.getBean("pluginManager");
		pluginManager.registerPlugin(this.getPlugin());
		
		SessionManager sessionManager = (SessionManager)context.getBean("sessionManager");
		GlobalConfig globalConfig = sessionManager.getGlobalConfig();
		UserManager userManager =(UserManager)context.getBean("userManager");		
		User admin = userManager.queryUserByUserId(User.USER_ADMIN);
		
		boolean first = RepositorySessionFactory.buildRepositoryFactory(globalConfig.getServerWorkFolder());
		try {
			if(first) {								
				RepositoryUtil.initRepository(admin.getUserId(),admin.getPassword());
			}
		} catch (LoginException e) {
			e.printStackTrace();
		} catch (RepositoryException e) {
			e.printStackTrace();
		}
		
		logger.debug("server started");
	}

	public void contextDestroyed(ServletContextEvent event) {		
		logger.debug("server closing");
		
		RepositorySessionFactory.closeRepositoryFactory();
		
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		PluginManager pluginManager = (PluginManager)context.getBean("pluginManager");
		pluginManager.unregisterPlugin(this.getPlugin());
		
		super.contextDestroyed(event);
		
		logger.debug("server closed");		
	}
	
	private Plugin getPlugin() {
		Plugin plugin = new Plugin();
		
		plugin.setId("org.jwos.file.war");
		plugin.setName("FileManager");
		plugin.setType(Plugin.TYPE_SYSTEM);
		plugin.setPath("file");
		plugin.setOrder(100);		
		
		plugin.addResource(new PluginResource("js/FileManager.Controls.js"));
		plugin.addResource(new PluginResource("js/FileManager.FileManager.js"));
		plugin.addResource(new PluginResource("js/FileManager.Menues.js"));
		plugin.addResource(new PluginResource("js/FileManager.Uploader.js"));
		plugin.addResource(new PluginResource("js/FileManager.RemoteMethods.js"));
		plugin.addResource(new PluginResource("js/FileManager.Module.js"));
		plugin.addResource(new PluginResource(PluginResource.TYPE_STYLE,"style/filemanager.css"));
		plugin.addResource(new PluginResource(PluginResource.TYPE_STYLE,"style/upload.css"));
		plugin.addResource(new PluginResource(PluginResource.TYPE_STYLE,"style/icons.css"));
		plugin.addResource(new PluginResource(PluginResource.TYPE_STYLE,"style/filetypes.css"));
		
		plugin.setLocaleResource(Locale.ENGLISH, new PluginResource("js/locale/FileManager.Messages_en.js"));
		plugin.setLocaleResource(Locale.SIMPLIFIED_CHINESE, new PluginResource("js/locale/FileManager.Messages_zh_CN.js"));
		plugin.setLocaleResource(Locale.JAPANESE, new PluginResource("js/locale/FileManager.Messages_ja.js"));
		
		return plugin;
	}
}
